import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
Vue.config.productionTip = false
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  mutations: {
    addFn(state, index) {
      state.goodList[index].num++
    },
    lessFn(state, index) {
      if (state.goodList[index].num === 1) return
      state.goodList[index].num--
    },
    delFn(state, index) {
      state.goodList.splice(index, 1)
    },
  },
  getters: {
    // isAll(state) {
    //   return state.goodList.checked = false
    // },
    sum(state) {
      return state.goodList.reduce((sum, item) => {
        if (item.checked) {
           sum += item.price * item.num
        }
      return sum
      },0)
    }
  },
})
new Vue({
  store,
  render: (h) => h(App),
}).$mount('#app')
